#SMSBOT
#import the module inbox
import inbox
#import the module e32
import e32
#import the module appuifw
import appuifw
#import the module messaging
import messaging
 
appuifw.app.title=u"SMS Assistant"
# Sets application titile
  
# Defines exit function
def exit_key_handler():
    app_lock.signal()

#Define autoreply function
def read_sms(id):
 
	e32.ao_sleep(0.1)        
	# create once more an instance of the inbox() class
	i=inbox.Inbox()
	# read the content out of the message that has just arrived
	sms_text = str(i.content(id))
	# display the content inside a pop-up note    
	print sms_text 
	m = i.sms_messages()  # all message ID's
	# import rules from csv file
	from csv import reader
	d = dict(row for row in reader(open('e:\\data.csv'), delimiter=',', quotechar='"'))
	out = (d[sms_text])
	# send reply message
	if sms_text:
		 messaging.sms_send(i.address(m[0]),out)
		
L = [u'Start', u'Exit']
 
# create the selection list
index = appuifw.selection_list(choices=L , search_field=1)
 
# Trigger action on index selection
if index == 0:
	i=inbox.Inbox()
        print "Waiting for Incomming SMS"
	i.bind(read_sms)
 
if index == 1:
	exit_key_handler()
	
 
app_lock = e32.Ao_lock()
 
appuifw.app.exit_key_handler = exit_key_handler
app_lock.wait()